package com.dingding.special.user.login.dao.impl;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.daox.IBaseDaoX;
import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.beans.special.SpecialOrderdesignateset;
import com.dingding.common.core.beans.special.Specialuseraccount;
import com.dingding.common.core.beans.special.Specialuserinfo;
import com.dingding.common.core.beans.special.Specialuserlogin;
import com.dingding.common.core.beans.special.Specialuserposition;
import com.dingding.common.core.beans.special.Specialuservehicleinfo;
import com.dingding.common.core.cache.base.JedisUtil;
import com.dingding.common.core.cache.enums.DbEnum;
import com.dingding.common.core.cache.enums.UserInfoKeyEnum;
import com.dingding.common.core.util.JSONUtil;
import com.dingding.special.user.login.dao.IUserLoginAddUserInfoToCache;

/**
 * 
 * @ClassName: UserLoginGetUserInfoDaoImpl 
 * @Description:用户登录添加所有用户信息到缓存中
 * @author CHEHAUNBO 
 * @date 2016年8月22日 下午10:08:14
 * @since V2.4.0
 * 
 */
@Component("addUserInfoToCacheDao")
public class AddUserInfoToCacheDaoImpl implements IUserLoginAddUserInfoToCache {

	private static Logger log = Logger.getLogger(AddUserInfoToCacheDaoImpl.class);

	@Resource(name = "criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;

	@Resource(name = "baseDaoX")
	private IBaseDaoX baseDaoX;

	/**
	 * 
	 * 获取用户详细信息,并添加到缓存中
	 * 
	 * @Title: getSpecialUserinfo 
	 * @param userId 用户ID
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	public void addSpecialUserinfoToCache(String userId) {

		Criterion criterion = Restrictions.eq("userId", userId);
		try {
			Specialuserinfo specialuserinfo = criteriaBaseDao.queryCriteriaListUnique(Specialuserinfo.class, criterion);
			if (specialuserinfo != null) {
				JedisUtil.setValue(UserInfoKeyEnum.SpecialUserInfo_ + userId, JSONUtil.toJSONString(specialuserinfo),
						DbEnum.SpecialUserDb.getValue());
			}
		} catch (Exception e) {
			log.error("用户登录获取用户详细信息出现异常！", e);
		}

	}

	/**
	 * 
	 * 获取用户位置信息,并添加到缓存中
	 * 
	 * @Title: getSpecialUserLogin 
	 * @param userId 用户ID
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	public void addSpecialuserpositionToCache(String userId) {

		Criterion criterion = Restrictions.eq("userId", userId);
		try {

			Specialuserposition specialuserposition = criteriaBaseDao.queryCriteriaListUnique(Specialuserposition.class,
					criterion);

			if (specialuserposition != null) {
				JedisUtil.setValue(UserInfoKeyEnum.SpecialUserPosition_ + userId,
						JSONUtil.toJSONString(specialuserposition), DbEnum.SpecialUserDb.getValue());
			}

		} catch (Exception e) {
			log.error("用户登录获取用户位置信息出现异常！", e);
		}

	}

	/**
	 * 
	 * 获取订单指派设置信息,并添加到缓存中
	 * @Title: addDesignateSetToCache 
	 * @param userId 用户ID
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	public void addDesignateSetToCache(String userId) {

		Criterion criterion = Restrictions.eq("userId", userId);
		try {

			SpecialOrderdesignateset specialOrderdesignateset = criteriaBaseDao
					.queryCriteriaListUnique(SpecialOrderdesignateset.class, criterion);
			if (specialOrderdesignateset != null) {
				JedisUtil.setValue(UserInfoKeyEnum.SpecialUserDesignateSet_ + userId,
						JSONUtil.toJSONString(specialOrderdesignateset), DbEnum.SpecialUserDb.getValue());
			}

		} catch (Exception e) {
			log.error("用户登录获取用户指派设置出现异常！", e);
		}

	}

	/**
	 * 
	 * 获取专车车主车辆信息
	 * @Title: addUserVehicleInfoToCache 
	 * @param userId 用户ID
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	public void addUserVehicleInfoToCache(String userId) {

		Criterion criterion = Restrictions.eq("userId", userId);
		try {

			Specialuservehicleinfo specialuservehicleinfo = criteriaBaseDao
					.queryCriteriaListUnique(Specialuservehicleinfo.class, criterion);

			if (specialuservehicleinfo != null) {
				JedisUtil.setValue(UserInfoKeyEnum.SpecialUserInfo_ + userId,
						JSONUtil.toJSONString(specialuservehicleinfo), DbEnum.SpecialUserDb.getValue());
			}

		} catch (Exception e) {
			log.error("用户登录获取车辆信息出现异常！", e);
		}

	}

	/**
	 * 
	 * 添加所有用户信息到缓存中
	 * 
	 * @Title: addAllUserInfoToCache 
	 * @param uuid 用户ID 
	 * @param specialuserlogin 用户登录信息
	 * @param specialuserinfo 用户详细信息
	 * @param specialuserposition 用户位置信息
	 * @param specialOrderdesignateset 指派订单设置
	 * @param specialuseraccount  
	 * @author CHEHAUNBO
	 * 
	 */
	public boolean addAllUserInfoToCache(String uuid, Specialuserlogin specialuserlogin,
			Specialuserinfo specialuserinfo, Specialuserposition specialuserposition,
			SpecialOrderdesignateset specialOrderdesignateset, Specialuseraccount specialuseraccount) {

		try {
			// 添加缓存
			// 用户登录信息
			// JedisUtil.setMap(UserInfoKeyEnum.SpecialUserLogin_ + uuid,
			// MapUtil.objToMapConvert(specialuserlogin),
			// DbEnum.SpecialUserDb.getValue());
			// 用户详细信息
			// JedisUtil.setMap(UserInfoKeyEnum.SpecialUserInfo_ + uuid,
			// MapUtil.objToMapConvert(specialuserinfo),
			// DbEnum.SpecialUserDb.getValue());
			JedisUtil.setValue(UserInfoKeyEnum.SpecialUserPosition_ + uuid, JSONUtil.toJSONString(specialuserposition),
					DbEnum.SpecialUserDb.getValue());
			// 用户指派订单设置
			JedisUtil.setValue(UserInfoKeyEnum.SpecialUserDesignateSet_ + uuid,
					JSONUtil.toJSONString(specialOrderdesignateset), DbEnum.SpecialUserDb.getValue());
			// // 用户账户设置
			// JedisUtil.setMap(UserInfoKeyEnum.SpecialUserAccount_ + uuid,
			// MapUtil.objToMapConvert(specialuseraccount),
			// DbEnum.SpecialUserDb.getValue());
			return true;
		} catch (Exception e) {
			log.error("添加所有用户信息到缓存中出现异常！", e);
			return false;
		}
	}
}
